package LC;

import java.util.HashSet;

/**
 * https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
 * Given a string, find the length of the longest substring without repeating characters.
 * Examples:
 * Given "abcabcbb", the answer is "abc", which the length is 3.
 * Given "bbbbb", the answer is "b", with the length of 1.
 * Given "pwwkew", the answer is "wke", with the length of 3.
 * Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
 */
public class LC_003_LongestSubstringWithoutRepeatingCharacters_TwoPointers {
    public static void main(String[] args) {
        String s = "abaxy";
//        String s = "pwwkew";
//        String s = "mabcdabcdmmm";
        System.out.println(lengthOfLongestSubstring(s));
    }

    private static int lengthOfLongestSubstring(String s) {
        int n = s.length();
        HashSet<Character> hs = new HashSet<>();
        int i = 0, j = 0, maxLen = 0;
        while (i < n && j < n) {
            if (!hs.contains(s.charAt(j))) {
                hs.add(s.charAt(j++));
                maxLen = Math.max(maxLen, j - i);
            } else {
                hs.remove(s.charAt(i++));
            }
            System.out.println(i + " " + j + " " + (j - i));
        }
        return maxLen;
    }
}
